package br.com.admin.dao;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import br.com.admin.datatable.DataSourceCountSQLBuilder;
import br.com.admin.datatable.DataSourceSQLBuilder;
import br.com.admin.domain.DataSource;

@Repository
@SuppressWarnings("unchecked")
public class DataSourceDAO extends BaseDAO {
	
	public void save(DataSource entity){
		this.persist(entity);
	}	
	
	public List<DataSource> listForDataTable(List<String> searchColumns, String searchValue, String orderColumn, String orderDirection, int firstPosition, int numberOfRecords){
		DataSourceSQLBuilder whereClause = new DataSourceSQLBuilder();
		Query query = entityManager.createQuery(whereClause.execute(searchColumns, searchValue, orderColumn, orderDirection));
		query.setFirstResult(firstPosition);
		query.setMaxResults(numberOfRecords);
		return query.getResultList();
	}
	
	public Long countForDataTable(List<String> searchColumns, String searchValue){
		DataSourceCountSQLBuilder whereClause = new DataSourceCountSQLBuilder();
		Query query = entityManager.createQuery(whereClause.execute(searchColumns, searchValue));
		return Long.class.cast(query.getSingleResult());
	}
	
}
